<!DOCTYPE html>
<html>
<head>
  <link rel="chrome-webstore-item">
</head>
<body>
<script>
  console.log('Page loaded');

  function tryInlineInstall() {
    try {
      chrome.webstore.install(
          undefined,
          function() {
            console.log('did not expect install success');
            window.domAutomationController.send(false);
          },
          function(error, errorCode) {
            if (error.indexOf('verified site') != -1 &&
                errorCode == 'notPermitted') {
              console.log('Ran test, sending response');
              window.domAutomationController.send(true);
              console.log('Test complete');
            } else {
              console.log('Unexpected error: ' + error);
              window.domAutomationController.send(false);
            }
          });
    } catch (e) {
      console.log('Unexpected exception: ' + e);
      window.domAutomationController.send(false);
      throw e;
    }
  }

  function runTest1(galleryUrl) {
    console.log('In runTest1');

    // Initially we point at an extension that has a verified domain of app.com
    // but we're on nonapp.com (Link URL has to be generated dynamically in
    // order to include the right port number. The ID corresponds to the data
    // in the "extension" directory.)
    document.getElementsByTagName('link')[0].href =
        galleryUrl + '/detail/ecglahbcnmdpdciemllbhojghbkagdje';

    tryInlineInstall();
  }

  function runTest2(galleryUrl) {
    console.log('In runTest2');

    // Now try a different extension, which doesn't have a verified domain at
    // all (it should not be allowed)
    document.getElementsByTagName('link')[0].href =
        galleryUrl + '/detail/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';

    tryInlineInstall();
  }
</script>

</body>
</html>
